﻿//  Copyright (c) 2010 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.UI;

namespace DNA.Mvc.jQuery
{
    //[Obsolete]
    public class NavigableView :TemplateViewComponent, INavigable
    {
        #region private
        private string _title = "";
        private object _value = "";
        private string imageUrl = "";
        private string tooltip = "";
        private string navigateUrl = "";
        private string target = "";
        #endregion

        public NavigableView() { }

        public NavigableView(INavigable node)
        {
            this.Bind(node);
            model = node;
        }

        private void Bind(INavigable node)
        {
            Title = node.Title;
            NavigateUrl = node.NavigateUrl;
            ImageUrl = node.ImageUrl;
            target = node.Target;
            tooltip = node.Description;
            Value = node.Value;
        }

        private object model = null;
        
        [ScriptIgnore]
        public object Model
        {
            get { return model; }
            set
            {
                if ((value as INavigable) != null)
                    this.Bind((INavigable)value);
                model = value;
            }
        }

        /// <summary>
        /// Gets/Sets the navigate url of the node.
        /// </summary>
        public string NavigateUrl
        {
            get { return navigateUrl; }
            set { navigateUrl = value; }
        }

        /// <summary>
        /// Gets/Sets the tooltip text of the node.
        /// </summary>
        public string ToolTip
        {
            get { return tooltip; }
            set { tooltip = value; }
        }
        /// <summary>
        /// Gets/Sets the open window target
        /// </summary>
        public string Target
        {
            get { return target; }
            set { target = value; }
        }
        
        /// <summary>
        /// Gets/Sets the additional value.
        /// </summary>
        public object Value
        {
            get { return this._value; }
            set { this._value = value; }
        }

        /// <summary>
        /// Gets/Sets the image url.
        /// </summary>
        public string ImageUrl
        {
            get { return imageUrl; }
            set { imageUrl = value; }
        }

        /// <summary>
        /// Gets/Sets the node text content.
        /// </summary>
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        #region INavigate members

        string INavigable.Description
        {
            get { return this.tooltip; }
        }

        string INavigable.Title
        {
            get { return this.Title; }
        }

        string INavigable.NavigateUrl
        {
            get { return this.NavigateUrl; }
        }

        string INavigable.ImageUrl
        {
            get { return this.ImageUrl; }
        }

        string INavigable.Target
        {
            get { return this.Target; }
        }

        object INavigable.Value
        {
            get { return this.Value; }
        }

        #endregion
        
        [ScriptIgnore]
        public override string TagName
        {
            get
            {
                return "li";
            }
        }

        public override void RenderContent(HtmlTextWriter writer)
        {
            if (Template.IsEmpty)
            {
                var builder = new NodeUIBuilder();
                builder.WriteNodeContent(this);
                writer.Write(builder.ToString());
            }
            else
            {
                //if (Value != null)
                   // writer.Write("<input type=\"hidden\" value=\"\"/>");
                writer.WriteBeginTag("input");
                writer.WriteAttribute("type", "hidden");
                if (Value != null)
                    writer.WriteAttribute("value", Value.ToString());
                writer.Write(Html32TextWriter.SelfClosingTagEnd);
                Template.WriteTo( writer);
            }
        }
    }

    ///// <summary>
    ///// 
    ///// </summary>
    //public class NavigtableViewComponent<TModel> : DataBindableCompnent<TModel>
    //    where TModel:class
    //{
    //    public override string TagName
    //    {
    //        get
    //        {
    //            return "li";
    //        }
    //    }

    //    public NavigtableViewComponent() { }

    //    public NavigtableViewComponent(TModel dataItem) : base(dataItem) { }

    //    public override void RenderContent(System.Web.UI.HtmlTextWriter writer)
    //    {
    //        if (Template.IsEmpty)
    //        {
    //            if (DataItem != null)
    //            {
    //                var _data = DataItem as INavigtable;
    //                if (_data != null)
    //                {
    //                    var builder = new NodeUIBuilder();
    //                    builder.WriteNodeContent(_data);
    //                    writer.Write(builder.ToString());
    //                }
    //            }
    //        }
    //        else
    //            base.RenderContent(writer);
    //    }
    //}

    //public class NavigtableViewComponent : NavigtableViewComponent<object> { }
}
